//
//  ModelSuperObject.m
//  Test
//
//  Created by SL on 14-3-7.
//  Copyright (c) 2014年 Sheng Long. All rights reserved.
//

#import "ModelBaseClass.h"

@implementation ModelBaseClass

//适用于此方法的所有属性名称必须小写==字典dataDic的key值转换成小写
-(id)init:(NSDictionary*)dataDic
{
    self = [super init];
    if (self) {
        //
        NSArray *keys = [dataDic allKeys];
        //
        for (NSString * key in keys) {
            
            //
            if (key.length==0) {
                continue;
            }
            
            //转换成属性的set方法,首字母需大写,其余字母均小写
            NSString *text1 = [[key substringWithRange:NSMakeRange(0, 1)] uppercaseString];
            NSString *text2 = [[key substringWithRange:NSMakeRange(1, key.length-1)] lowercaseString];
            NSString *selectString = [NSString stringWithFormat:@"set%@%@:",text1,text2];
            SEL selector = NSSelectorFromString(selectString);
            
            //判断是否存在此属性，赋值
            if ([self respondsToSelector:selector]) {
                //
                NSObject *dicValue = [dataDic objectForKey:key];
                
                if ([dicValue isKindOfClass:[NSNumber class]]) {
                    // NSNumber 类型数据，需转换成 NSString
                    dicValue = [NSString stringWithFormat:@"%@",dicValue];
                }else if ([dicValue isKindOfClass:[NSNull class]]){
                    // NSNull 类型数据，转换成 nil
                    dicValue = nil;
                }
                //
//                [self performSelector:selector withObject:[self flattenHTML:((NSString*)dicValue)]];
                if ([self respondsToSelector:selector]) {
                    [self performSelector:selector withObject:dicValue];
                }
//                [self performSelector:@selector(sss) withObject:nil];
            }
        }
    }
    return self;
}

//去除HTML标签
- (NSString *)flattenHTML:(NSString *)html {
    
    NSScanner *theScanner;
    NSString *text = nil;
    
    theScanner = [NSScanner scannerWithString:html];
    
    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL] ;
        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text] ;
        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        html = [html stringByReplacingOccurrencesOfString:
                [NSString stringWithFormat:@"%@>", text]
                                               withString:@""];
    } // while //
    return html;
}

@end

@implementation UserMsg
@end










